package com.ccsoft.atools.service;


import com.ccsoft.atools.dao.CashMapper;
import com.ccsoft.atools.model.Cash;
import com.ccsoft.atools.model.ContactCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CashServiceImpl implements CashService {

    @Autowired
    private CashMapper dao;

    @Autowired
    private ContactCompanyService ccService;

    @Override
    public int deleteByPrimaryKey(Integer iid) {
        return dao.setState(iid, 0);
    }

    @Override
    public Cash insert(Cash record) {
        record = updateComName(record);
        int ret = dao.insert(record);
        return record;
    }

    @Override
    public Cash insertSelective(Cash record) {
        record = updateComName(record);
        int ret = dao.insertSelective(record);
        return selectByPrimaryKey(record.getIid());
    }

    @Override
    public Cash selectByPrimaryKey(Integer iid) {
        return dao.selectByPrimaryKey(iid);
    }

    @Override
    public Cash updateByPrimaryKeySelective(Cash record) {
        record = updateComName(record);
        int ret = dao.updateByPrimaryKeySelective(record);
        return selectByPrimaryKey(record.getIid());
    }

    @Override
    public Cash updateByPrimaryKey(Cash record) {
        record = updateComName(record);
        int ret = dao.updateByPrimaryKey(record);
        return record;
    }

    @Override
    public List<Cash> selectAll() {
        return dao.selectAll();
    }

    @Override
    public int setState(int iid, int state) {
        return dao.setState(iid, state);
    }

    @Override
    public int restoreDeleted(int iid) {
        return dao.setState(iid, 1);
    }

    public Cash updateComName(Cash record) {
        if (record == null) return null;
        String sid = record.getCompany_sid();
        if (sid == null || sid.length() <= 0) return record;

        ContactCompany cc = ccService.selectBySid(sid);
        if (cc == null) return record;
        record.setCompany_name(cc.getCompany_name());
        return record;
    }
}